package me.tonywang.common.controller;

import me.tonywang.common.result.PageInfo;
import me.tonywang.common.result.QueryParam;
import me.tonywang.config.UiParamProperties;
import me.tonywang.common.service.ICommonQryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 通用的控制器
 *
 * @author L.cm
 */
@Controller
public class CommonsController {

    protected Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private ICommonQryService commonQryService;

    /**
     * 图标页
     */
    @GetMapping("icons.html")
    public String icons() {
        return "icons";
    }



/*    @Value("${ui.pageSizeParamName:pageSize1}")
    String pageSizeParamName ;*/

    @Autowired
    private UiParamProperties uiParamProperties  ;

    @GetMapping("/testVal")
    @ResponseBody
    public String testVAL(@RequestParam Map<String, Object> condition) {
        String pageSizeParamName  = uiParamProperties.getPageSizeParamName();
        String pageNumberParamName =  uiParamProperties.getPageNumberParamName()  ;
        int rows =  Integer.valueOf((String)condition.getOrDefault(pageSizeParamName,"20"));
        int page =  Integer.valueOf((String)condition.getOrDefault(pageNumberParamName,"1"));
        System.out.println("rows " + rows );
        System.out.println("page " + page );
        return "" ;
    }

    @PostMapping("/commons/pageqry")
    @ResponseBody
    public PageInfo dataGrid(@RequestParam  String statement,
                             @RequestParam Map<String, Object> condition,
                             @RequestParam(required=false) String sort,
                             @RequestParam(required=false) String order,
                             QueryParam queryParam
                             ) {
        String pageSizeParamName  = uiParamProperties.getPageSizeParamName();
        String pageNumberParamName =  uiParamProperties.getPageNumberParamName()  ;
        int rows =  Integer.valueOf((String)condition.getOrDefault(pageSizeParamName,"20"));
        int page =  Integer.valueOf((String)condition.getOrDefault(pageNumberParamName,"1"));
        logger.debug("page :" + page);
        logger.debug("rows :" + rows);
        logger.debug("condition :" + condition);
        logger.debug("queryParam :" + queryParam);
        List<Map<String, String>> filterRules = queryParam.getFilterRules();
        if(!CollectionUtils.isEmpty(filterRules)) {
            condition.put("filterRules", filterRules);
        }
        return commonQryService.dataGrid(statement, condition, page, rows, sort, order);
    }

    @PostMapping("/commons/dataList")
    @ResponseBody
    public List dataGrid(@RequestParam(required=true) String statement,
                         @RequestParam Map<String, Object> condition) {
        logger.debug("condition :" + condition);
        return commonQryService.dataList(statement, condition );
    }

    /**
     * 菜单树
     *
     * @return
     */
    @RequestMapping("/commons/tree")
    @ResponseBody
    public Object tree(@RequestParam(required=true) String statement,
                       @RequestParam Map<String, Object> condition ) {
        return commonQryService.selectTree(statement, condition );
    }

}
